package com.zzyl.nursing.service.impl;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.zzyl.common.core.domain.entity.SysUser;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
/**
 * 报警数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-05
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper,AlertData> implements IAlertDataService
{
    @Autowired
    private AlertDataMapper alertDataMapper;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询报警数据
     * 
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     * 
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData)
    {
        alertData.setUserId(SecurityUtils.getLoginUser().getUserId());
        return alertDataMapper.selectAlertDataList(alertData);
    }

    /**
     * 新增报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData)
    {
        return save(alertData)?1:0;
    }

    /**
     * 修改报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData)
    {
        return updateById(alertData)?1:0;
    }

    /**
     * 批量删除报警数据
     * 
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids))?1:0;
    }

    /**
     * 删除报警数据信息
     * 
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return removeById(id)?1:0;
    }

    /**
     * 处理报警数据
     *
     * @param alertData
     */
    @Override
    public void handleAlertData(AlertData alertData) {
        if (ObjectUtil.isEmpty(alertData) || ObjectUtil.isEmpty(alertData.getId())) {
            return;
        }
        AlertData alertDataDb = lambdaQuery().eq(AlertData::getId, alertData.getId()).one();
        if (ObjectUtil.isEmpty(alertDataDb)) {
            return;
        }
        List<AlertData> alertDataList = lambdaQuery().eq(AlertData::getIotId, alertDataDb.getIotId())
                .eq(AlertData::getAlertRuleId, alertDataDb.getAlertRuleId())
                .eq(AlertData::getLocationType, alertDataDb.getLocationType())
                .eq(AlertData::getPhysicalLocationType, alertDataDb.getPhysicalLocationType())
                .eq(AlertData::getAccessLocation, alertDataDb.getAccessLocation())
                .eq(AlertData::getCreateTime, alertDataDb.getCreateTime())
                .list();
        SysUser user = sysUserService.lambdaQuery().eq(SysUser::getUserId, SecurityUtils.getUserId()).one();
        if (ObjectUtil.isEmpty(user)) {
            return;
        }

        alertDataList.forEach(
                alertDataItem -> {
                    alertDataItem.setStatus(1);
                    alertDataItem.setProcessingResult(alertData.getProcessingResult());
                    alertDataItem.setProcessingTime(alertData.getProcessingTime());
                    alertDataItem.setProcessorId(user.getUserId());
                    alertDataItem.setProcessorName(user.getNickName());
                }
        );

        updateBatchById(alertDataList);
    }

}
